package cn.com.dcsgo.api.fallback;

import cn.com.dcsgo.api.client.ItemClient;
import cn.com.dcsgo.api.dto.ItemDTO;
import cn.com.dcsgo.api.dto.OrderDetailDTO;
import cn.com.dcsgo.exception.BizIllegalException;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.context.annotation.Bean;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Slf4j
public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {
    @Override
    public ItemClient create(Throwable cause) {
        log.error("item-service调用失败", cause);
        return new ItemClient() {
            @Override
            public List<ItemDTO> queryItemsByIds(Collection<Long> ids) {
                log.error("商品查询失败!", cause);
                return Collections.emptyList();
            }

            @Override
            public void deductStock(List<OrderDetailDTO> items) {
                log.error("扣减商品库存失败!", cause);
                throw new BizIllegalException("扣减商品库存失败!");
            }

            @Override
            public void addStock(List<OrderDetailDTO> items) {
                log.error("商品库存回滚失败!", cause);
                throw new BizIllegalException("商品库存回滚失败!");
            }

            @Override
            public Integer getStockById(Long id) {
                return 0;
            }
        };
    }
}
